// 导入组件
import Vue from "vue";
import Router from "vue-router";
// 登录
import login from "@/views/login";
// 首页
import index from "@/views/index";
/**
 * 基础菜单
 */
// 商品管理
import Goods from "@/views/goods/Goods";
// 机器信息管理
import Machine from "@/views/machine/Machine";
// 货道信息管理
import MachineAisle from "@/views/machine/MachineAisle";

/**
 * 系统管理
 */
// 用户管理
import user from "@/views/system/user";
// 菜单管理
import Module from "@/views/system/Module";
// 角色管理
import Role from "@/views/system/Role";
// 公司管理
import Dept from "@/views/system/Dept";
// 系统环境变量
import Variable from "@/views/system/Variable";
// 权限管理
import Permission from "@/views/system/Permission";
/**
 * 支付管理
 */
// 支付配置信息
import MachineConfig from "@/views/machine/MachineConfig";

/**
 * 数据监控
 */
// 监控查询
import druidLogin from "@/views/druid/login";

// 图表界面
import statistics from "@/views/charts/statistics";

//正式页面
//分子展示页面
import Exhibition from "../views/exhibition/Exhibition";
import MolecularDesign from "../views/add/MolecularDesign";
//详情页面
import DetailsPage from "../views/add/DetailsPage";
import synthesisDetail from "../views/add/synthesisDetail.vue";
import Cedetails from "../views/add/Cedetails.vue";
import MolecularCompose from "../views/add/MolecularCompose";
import Bioassay from "../views/add/Bioassay";
import SearchBarCode from "../views/barCode/SearchBarCode";
import Download from "../views/excel/Download";
import UploadCompound from "../views/excel/UploadCompound";
import {getCookie, setCookie} from "../utils/util";
import GroupManaged from "../views/group/GroupManaged";
import UserManaged from "../views/group/UserManaged";
import PowerManaged from "../views/group/PowerManaged";
import GroupMember from "../views/group/GroupMember";
import PersonalCenter from "../views/PersonalCenter";
import axios from "axios";

// 启用路由
Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const routes = [
  {
    path: "/",
    name: "",
    component: login,
    hidden: true,
    meta: {
      requireAuth: false
    }
  },
  {
    path: "/login",
    name: "登录",
    component: login,
    hidden: true,
    meta: {
      requireAuth: false
    }
  },
  {
    path: "/index",
    name: "首页",
    component: index,
    iconCls: "el-icon-tickets",
    children: [
      {
        path: "/goods/Goods",
        name: "商品管理",
        component: Goods,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/machine/Machine",
        name: "机器信息管理",
        component: Machine,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/machine/MachineAisle",
        name: "货道信息管理",
        component: MachineAisle,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/system/user",
        name: "用户管理",
        component: user,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/system/Module",
        name: "菜单管理",
        component: Module,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/system/Role",
        name: "角色管理",
        component: Role,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/system/Dept",
        name: "公司管理",
        component: Dept,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/system/Variable",
        name: "系统环境变量",
        component: Variable,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/system/Permission",
        name: "权限管理",
        component: Permission,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/machine/MachineConfig",
        name: "支付配置信息",
        component: MachineConfig,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/druid/login",
        name: "监控查询",
        component: druidLogin,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/charts/statistics",
        name: "数据可视化",
        component: statistics,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/Exhibition",
        name: "展示页面",
        component: Exhibition,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/DetailsPage/:id",
        name: "DetailsPage",
        component: DetailsPage,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/synthesisDetail/:id",
        name: "synthesisDetail",
        component: synthesisDetail,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/Cedetails/:id",
        name: "Cedetails",
        component: Cedetails,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/add/molecularDesign",
        name: "分子设计数据输入",
        component: MolecularDesign,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/add/molecularCompose",
        name: "分子合成数据输入",
        component: MolecularCompose,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/add/bioassay",
        name: "生物测试数据输入",
        component: Bioassay,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/searchBarCode",
        name: "查询存储样品瓶",
        component: SearchBarCode,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/uploadCompound",
        name: "导入数据",
        component: UploadCompound,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/downloadCompound",
        name: "导出数据",
        component: Download,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/userManaged",
        name: "用户管理",
        component: UserManaged,
        meta: {
          requireAuth: true
        }
      }, {
        path: "/groupManaged",
        name: "组管理",
        component: GroupManaged,
        meta: {
          requireAuth: true
        }
      }, {
        path: "/powerManaged/:id",
        name: "权限管理",
        component: PowerManaged,
        meta: {
          requireAuth: true
        }
      }, {
        path: "/groupMember/:id",
        name: "小组成员",
        component: GroupMember,
        meta: {
          requireAuth: true
        }
      },
      {
        path: "/PersonalCenter",
        name: "个人中心",
        component: PersonalCenter,
        meta: {
          requireAuth: true
        }
      },
    ]
  }
]
const router = new Router({
  routes
})

// 挂载路由导航守卫
// 验证不是很完善 乱写一个token也能进去※

router.beforeEach((to, before, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数 表示放行
  // next() 放行 next(‘/login’) 强制跳转
  // 获取token
  // 无token代表没登录

  // 向服务器发送请求 如果token无效 删除token 返回登录页面
  // 如果
  // const {data: res} = this.$axios.get("getUserIdentity", {
  //   params: this.queryInfo
  // }).then(re=>{
  //   console.log(res)
  //   console.log(re)
  //   console.log('XXXXXXXXXX')
  // }

  const tokenStr = getCookie('token')
  //如果进的是login页面/主页
  //如果没token next() 如果有token 去MD页面
  if (to.path === '/login' || to.path === '/') {
    if (!tokenStr) {
      return next()
    } else {
      return next('/add/molecularDesign')
    }
  }
  //如果进的不是login页面
  // 如果没有token 强制跳转到login 如果有token next()
  if (!tokenStr) return next('/login')
  next()
})
axios.interceptors.response.use(function (res) {

  // let token=Storage.localGet('token')


  // console.log(res.data)
  if (res.data.mag === 'token过期'||res.data.mag === '无效签名'||res.data.mag === 'token无效'||res.data.mag === 'token算法不一致'
    ||res.data.mag === '凭据失效') {
    console.log('token过期了')
    setCookie('token', null, -1);
    // this.$router.push({path: "/login"}).then(r =>{
    // //
    // });
    console.log('token过期了2')
    window.location.href="/login"

  }
  //搞不定※
  // if (res.data.msg === '用户无权限!') {
  //   console.log('无权限')
  //   return "redirect:/add/molecularDesign"
  // }
  if (res.data.mag === '认证失败!尝试重新登陆') {
    console.log('认证失败了')
    setCookie('token', null, -1);
    // this.$router.push({path: "/login"}).then(r =>{
    // //
    // });
    window.location.href="/login"

  }
  return res
  //删除过期token

})

// 导出路由
export default router

// export default new Router({
//
//   routes: [
//     {
//       path: "/",
//       name: "",
//       component: login,
//       hidden: true,
//       meta: {
//         requireAuth: false
//       }
//     },
//     {
//       path: "/login",
//       name: "登录",
//       component: login,
//       hidden: true,
//       meta: {
//         requireAuth: false
//       }
//     },
//     {
//       path: "/index",
//       name: "首页",
//       component: index,
//       iconCls: "el-icon-tickets",
//       children: [
//         {
//           path: "/goods/Goods",
//           name: "商品管理",
//           component: Goods,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/machine/Machine",
//           name: "机器信息管理",
//           component: Machine,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/machine/MachineAisle",
//           name: "货道信息管理",
//           component: MachineAisle,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/system/user",
//           name: "用户管理",
//           component: user,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/system/Module",
//           name: "菜单管理",
//           component: Module,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/system/Role",
//           name: "角色管理",
//           component: Role,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/system/Dept",
//           name: "公司管理",
//           component: Dept,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/system/Variable",
//           name: "系统环境变量",
//           component: Variable,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/system/Permission",
//           name: "权限管理",
//           component: Permission,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/machine/MachineConfig",
//           name: "支付配置信息",
//           component: MachineConfig,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/druid/login",
//           name: "监控查询",
//           component: druidLogin,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/charts/statistics",
//           name: "数据可视化",
//           component: statistics,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/Exhibition",
//           name: "展示页面",
//           component: Exhibition,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/DetailsPage/:id",
//           name: "DetailsPage",
//           component: DetailsPage,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/synthesisDetail/:id",
//           name: "synthesisDetail",
//           component: synthesisDetail,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/Cedetails/:id",
//           name: "Cedetails",
//           component: Cedetails,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/add/molecularDesign",
//           name: "分子设计数据输入",
//           component: MolecularDesign,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/add/molecularCompose",
//           name: "分子合成数据输入",
//           component: MolecularCompose,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/add/bioassay",
//           name: "生物测试数据输入",
//           component: Bioassay,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/searchBarCode",
//           name: "查询存储样品瓶",
//           component: SearchBarCode,
//           meta: {
//             requireAuth: true
//           }
//         },
//         {
//           path: "/downloadCompound",
//           name: "导出数据",
//           component: Download,
//           meta: {
//             requireAuth: true
//           }
//         },

//       ]
//     }
//   ]
// });
//
//
